// pages/facilityDetail/facilityDetail.js
var app=getApp();
var mta = require('../../utils/mta_analysis.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    paymentImg: app.globalData._network_path + 'payment_img.png',
    code_id: '',//户号id
    contract_id: '',//户号的合同id
    open:false,//开关
    openfirst:true,//防止频繁点击
    info:{},
    is_load:false,

    addpopup:false,//自定义名称修改弹窗
    code_name:'', //缴费器名称
    phone: '',//客服电话
    device_no:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    mta.Page.init();
    that.setData({
      code_id: options.code_id,
      contract_id: options.contract_id,
    })
    that.equipmentDetail();
    that.getPhone();//获取客服电话
  },

  onShow: function () {

  },

  /*** 页面相关事件处理函数--监听用户下拉动作*/
  onPullDownRefresh: function () {
    var that = this;
    that.equipmentDetail();
  },


  // 开启添加弹窗
  addchannelpop: function (e) {
    this.setData({
      addpopup: true,
      code_name: '',
    })
  },
  // 取消弹窗
  abolish() {
    var that = this;
    that.setData({
      addpopup: false
    })
  },
  //缴费器名称输入
  inputNameFun(e) {
    var that = this;
    that.setData({
      code_name: e.detail.value
    })
  },
  //确认修改 缴费器名称
  sureSubmit() {
    var that = this;
    that.setData({
      addpopup: false
    })
    var code_id = that.data.code_id;
    var code_name = that.data.code_name;

    app.ajax({
      url: 'User/Equipmentlists/editName',
      data: { code_id: code_id, name: code_name },
      success: function (res) {
        app.showToast(res.data.message);
        if (res.data.code == 1000) {
          that.setData({
            ['info.code_name']: code_name,
          });
        }
      }
    })
    that.setData({
      addpopup: false
    })
  },


  /*** 缴费器详情*/
  equipmentDetail:function(){
    var that = this;
    var code_id = that.data.code_id;
    var contract_id = that.data.contract_id;
    app.ajax({
      url: "User/Equipmentlists/equipmentDetail",
      data: { code_id: code_id, contract_id: contract_id },
      success:function(res){
        wx.stopPullDownRefresh();
        if(res.data.code == 1000){
          that.setData({
            info:res.data.data,
          })
        }
        that.setData({
          is_load:true
        })
      }
    })
  },
  //开关
  chooseone(e){
    var that = this, index = e.currentTarget.dataset.index, openfirst = that.data.openfirst;
    var device_no = e.currentTarget.dataset.device_no;
    that.setData({
      device_no:device_no
    })
    var status = this.data.info.equipment_lists[index].status;
    var switch_machine = this.data.info.equipment_lists[index].switch_machine;
    if (status == 1) {
      app.showToast("您的缴费器已离线，无法进行该操作", 'none', 2500);
      return false;
    }
    if (openfirst == false) {
      app.showToast("为保护缴费器请勿频繁操作,请5秒后重试", 'none', 3000);
      return false;
    }
    var switch_machine_str = 'info.equipment_lists[' + index +'].switch_machine';
    if (switch_machine == 0) {
      that.powerOff(1)
      that.setData({
        [switch_machine_str]: 1,
        openfirst:false,
      })
    } else {
      that.powerOff(0)
      that.setData({
        [switch_machine_str]: 0,
        openfirst: false,
      })
    }
  },
  //发送指令
  //开关机
  powerOff(device_status) {
    var that = this;
    var device_status = device_status;
    var sn = that.data.device_no;
    app.ajax({
      url: 'Common/Device/switch_control',
      data: { "sn": sn, "device_status": device_status },
      success: function (res) {
        var msg;
        if (device_status == 0) {
          msg = '缴费器已关闭';
        } else {
          msg = '缴费器已开启';
        }
        wx.vibrateLong();
        app.showToast(msg, 'none', 2500);
        setTimeout(() => {
          that.setData({
            openfirst: true
          })
        }, 5000);
        return false;
        }

    })
  },
  //续费
  goRenew: function (e) {
    var code = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: '../user/renew/renew?code=' + code,
    })
  },
  //客服
  helpCenter() {
    wx.navigateTo({
      url: '../customService/customService',
    })
  },
  //申请售后
  applyAfterSale: function (e) {
    var that = this;
    var code_id = e.currentTarget.dataset.code_id;
    var contract_id = e.currentTarget.dataset.contract_id;
    wx.navigateTo({
      url: '../user/applyAfterSale/applyAfterSale?code_id=' + code_id + '&contract_id=' + contract_id,
    })
  },
  //解绑
  flDetele(e) {
    var that = this, code_id = e.currentTarget.dataset.code_id;
    wx.showModal({
      title: '是否解绑',
      content: '解绑之后将无法查看该缴费器信息',
      showCancel: true,
      cancelText: '算了吧',
      cancelColor: '#DF0101',
      confirmText: '解绑',
      confirmColor: '#FF9E7D',
      success: function (res) {
        //确定事件
        if (res.confirm == true) {
          app.ajax({
            url: 'User/Equipmentlists/unBindEquipment',
            data: { code_id: code_id },
            success: function (res) {
              if (res.data.code == 1000) {
                app.showToast('删除成功');
                setTimeout(()=>{
                  wx.navigateBack({
                    delta: 1
                  })
                },1000)
              } else {
                app.showToast(res.data.message);
              }
            }
          })
        }
        //取消事件
        if (res.cancel == true) {
        }
      },
    })
  },
  //跳转 使用记录
  navUsageRecord(e){
    wx.navigateTo({
      url: '../user/usageRecord/usageRecord?code=' + e.currentTarget.dataset.code,
    })
  },
  //跳转 缴费记录
  paymentRecord(e) {
    wx.navigateTo({
      url: '/pages/guidance/guidance?code=' + e.currentTarget.dataset.code,
    })
  },
  //获取客服电话
  getPhone: function () {
    var that = this;
    // Ajax请求
    app.ajax({
      url: 'Common/Common/getSystemConfig',
      data: { key: 'company_hotline' },
      success: function (res) {
        if (res.data.code == 1000) {
          var content = res.data.data;
          that.setData({
            phone: content,
          })
        }
      }
    })
  },
  //打电话
  callPhone(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
})
